#include "DopplerDisplayForm.h"
#include "ui_DopplerDisplayForm.h"
#include <QDirModel>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QMimeData>
#include <QDrag>
#include <QMessageBox>

const int MAX_ITEM_QTY = 50 ;
static const char* g_strDisplayMode[] =
{
    "3D Display" ,
    "2D Display" ,
    "All" , "Part" , "Probe and Wedge" , "Weld" , "Sonic Beams" ,
    "S(t) SCAN" , "S(s) SCAN" , "S(t)-A SCAN" , "S(t)-A SCAN" , "S(t)-A-A-A SCAN" ,
    "S(S)-A-A-A SCAN" , "S(t)-A-A-A SCAN" , "S(t)-A-A-A SCAN" ,"S(t)-A-A-A SCAN" ,
    "S(t)-A-A-A SCAN" ,
    "S(t)-A-A-A SCAN" ,
    "S(t)-A-A-A SCAN" ,
    "S(t)-A-A-A SCAN" ,
    "S(t)-A-A-A SCAN" ,
    "S(t)-A-A-A SCAN" ,
    "S(t)-A-A-A SCAN" ,
    "S(t)-A-A-A SCAN" ,
    "S(t)-A-A-A SCAN" ,
    "S(t)-A-A-A SCAN"
}  ;


DopplerDisplayForm::DopplerDisplayForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DopplerDisplayForm)
{
    ui->setupUi(this);
    CreateSettingView() ;
}

DopplerDisplayForm::~DopplerDisplayForm()
{
    delete ui;
}


void DopplerDisplayForm::CreateSettingView()
{
    model = new QStandardItemModel(2,1);
    model->setHeaderData(0, Qt::Horizontal, tr("Display Mode"));
    QStandardItem* _pItems[MAX_ITEM_QTY]  ;
    int i;
    int _nQty = sizeof(g_strDisplayMode) / sizeof(int) ;
    for( i = 0 ; i < _nQty; i++)
    {
        _pItems[i] = new QStandardItem(tr(g_strDisplayMode[i]));
        QString str = QString(":/icon/toolbar/resource/toobar/0-");
        QString tmp; tmp.sprintf("%02d.png", i+1) ;
        str += tmp ;
        QByteArray array = str.toLatin1() ;
        _pItems[i]->setIcon(QIcon(array.data()));
        _pItems[i]->setData(i , Qt::UserRole);
    }

    model->setItem(0, 0, _pItems[0]);
    model->setItem(1, 0, _pItems[1]);

    for(i = 2 ; i< 7 ; i++)
    {
        _pItems[0]->appendRow(_pItems[i]);
    }
    for(i = 7 ; i< _nQty ; i++)
    {
        _pItems[1]->appendRow(_pItems[i]);
    }


    QTreeView *tree = ui->treeView;
    tree->setModel (model);
    tree->expandAll();
    tree->viewport()->setAttribute(Qt::WA_StaticContents);
    connect(tree , SIGNAL(pressed(QModelIndex)) , SLOT(StartDrags(QModelIndex)))  ;
}

void DopplerDisplayForm::CreateDisplayView()
{


}


void DopplerDisplayForm::StartDrags(QModelIndex index)
{
    // Get item string and id
    const QAbstractItemModel* model = index.model() ;
    QString _str1  = model->data(index , Qt::DisplayRole).toString()  ;
    int _nId = model->data(index , Qt::UserRole).toInt() ;
    QVariant icon = model->data(index , Qt::DecorationRole);
    QPixmap pixmap =  (icon.value<QIcon>()).pixmap(48 , 48);

    // setup MimeData
    QByteArray itemData;
    QDataStream dataStream(&itemData, QIODevice::WriteOnly);
    dataStream << _str1 << _nId ;

    QMimeData *mimeData = new QMimeData;
    mimeData->setData("DOPPLER_DISPLAY_ITEM", itemData);

    QDrag *drag = new QDrag(this);
    drag->setMimeData(mimeData);

    drag->setHotSpot(QPoint(pixmap.width()/2, pixmap.height()/2));
    drag->setPixmap(pixmap);

    drag->exec() ; //(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
}
